import * as apis from '@/apis/modules/transferManage.js';
import { message, uniHistory } from "@/common/utils.js"

const transferManage = {
	namespaced: true,
	state: {
		total: 0,
		list: [],
		hasData: true,
		pageNum: 1,
		targetAgentInfo: {},
		transferArr: [],

		transferInRecords: [],
		transferOutRecords: [],
		transferInRecordsPageNum: 1,
		transferOutRecordsPageNum: 1,
		hasTransferInRecords: true,
		hasTransferOutRecords: true
	},
	mutations: {
		save(state, {
			field,
			payload
		}) {
			if (Object.prototype.toString.call(payload) === '[object Object]') {
				state[field] = {
					...state[field],
					...payload
				}
			} else {
				state[field] = payload
			}
		}
	},
	actions: {
		getList({
			commit,
			state
		}, {
			deviceType,
			pageNum = 1,
			search = ''
		}) {
			apis.getList({
				deviceType,
				deviceStatus: '01',
				current: pageNum,
				size: 10,
				search
			}).then(({
				error,
				data
			}) => {
				if (error) return

				const {
					pages,
					records,
					total
				} = data
				const {
					list
				} = state
				let recordList = list

				if (pageNum > 1) {
					recordList = recordList.concat(records)
				} else {
					recordList = records || []
					commit('save', {
						field: 'hasData',
						payload: true
					})
				}

				if (pages === pageNum || pages === 0) commit('save', {
					field: 'hasData',
					payload: false
				})

				commit('save', {
					field: 'pageNum',
					payload: pageNum
				})
				commit('save', {
					field: 'total',
					payload: total
				})
				commit('save', {
					field: 'list',
					payload: recordList
				})
			})
		},

		getRecords({
			commit,
			state
		}, {
			deviceType,
			queryType,
			pageNum = 1,
			search
		}) {
			apis.getRecords({
				deviceType,
				queryType,
				current: pageNum,
				size: 10,
				search
			}).then(({
				error,
				data
			}) => {
				if (error) return

				const {
					pages,
					records
				} = data
				const {
					transferInRecords,
					transferOutRecords
				} = state
				let recordList = queryType === 'out' ? transferOutRecords : transferInRecords

				if (pageNum > 1) {
					recordList = recordList.concat(records)
				} else {
					recordList = records || []
					commit('save', {
						field: queryType === 'out' ? 'hasTransferOutRecords' :
							'hasTransferInRecords',
						payload: true
					})
				}

				if (pages === pageNum || pages === 0) commit('save', {
					field: queryType === 'out' ? 'hasTransferOutRecords' : 'hasTransferInRecords',
					payload: false
				})

				commit('save', {
					field: queryType === 'out' ? 'transferOutRecordsPageNum' :
						'transferInRecordsPageNum',
					payload: pageNum
				})
				commit('save', {
					field: queryType === 'out' ? 'transferOutRecords' : 'transferInRecords',
					payload: recordList
				})
			})
		},

		getAgentInfo({
			commit
		}, agentMobile) {
			return apis.getAgentInfo(agentMobile)
		},

		transferDevice({
			commit
		}, payload) {
			apis.transferDevice(payload)
				.then(({
					error,
					data
				}) => {
					if (error) return

					const pages = getCurrentPages()
					const transferIndex = pages[pages.length - 2]
					transferIndex.selectArr = []
					message.success({
						title: '划拨成功',
						cb: uniHistory.goBack
					})
				})
		},

		transferDevice2({
			commit
		}, payload) {
			apis.transferDevice2(payload)
				.then(({
					error,
					data
				}) => {
					if (error) return

					const pages = getCurrentPages()
					const transferIndex = pages[pages.length - 2]
					transferIndex.selectArr = []
					message.success({
						title: '划拨成功',
						cb: uniHistory.goBack
					})
				})
		},

		cancelTransfer({
			commit,
			state
		}, {
			uid,
			idx,
			currentTab,
			cb
		}) {
			apis.cancelTransfer({
					uid
				})
				.then(({
					error,
					data
				}) => {
					if (error) return

					message.success({
						title: '操作成功',
						cb
					})
					const list = state[currentTab === 'import' ? 'transferInRecords' : 'transferOutRecords']

					list[idx].transferStatus = '02'
					commit('save', {
						field: currentTab === 'import' ? 'transferInRecords' : 'transferOutRecords',
						payload: list
					})
				})
		},

		acceptTransfer({
			commit,
			state
		}, {
			uid,
			idx,
			currentTab,
			cb
		}) {
			apis.acceptTransfer({
					uid
				})
				.then(({
					error,
					data
				}) => {
					if (error) return

					message.success({
						title: '操作成功',
						cb
					})
					const list = state[currentTab === 'import' ? 'transferInRecords' :
						'transferOutRecords']

					list[idx].transferStatus = '01'
					commit('save', {
						field: currentTab === 'import' ? 'transferInRecords' : 'transferOutRecords',
						payload: list
					})
				})
		}
	}
}

export default transferManage
